<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title></title>
    </head>
    <body>
<!--        <pre>-->
        <?php
        require('lib/php/shl.php');
        set_time_limit(3);

        $sh = shCreate_2('table#personTable>',
            'tr>tr',
            'th/ :~s th/ :~s th/ :~s',
            'person name', 'title', 'salary',
            'for(/personRecs)',
            'tr> td#pn/ pn:~s/personName :/personName title:~s/title td/ :/title td/ :/salary pid:~d/pid < ',
            '//efor');

        $sh['personRecs'] =
            array(
                array(
                    'pid' => 2,
                    'personName' => 'Pop',
                    'title' => 'Mr',
                    'salary' => 2322232
                ),
                array(
                    'pid' => 2,
                    'personName' => 'James',
                    'title' => 'Mr',
                    'salary' => 2322
                ),
                array(
                    'pid' => 2,
                    'personName' => 'Jerry',
                    'title' => 'Mr',
                    'salary' => 23
                ),
                array(
                    'pid' => 2,
                    'personName' => 'Ivy',
                    'title' => 'Ms',
                    'salary' => 124
                ),
                array(
                    'pid' => 2,
                    'personName' => 'Anna',
                    'title' => 'Ms',
                    'salary' => 3
                ),
                array(
                    'pid' => 2,
                    'personName' => 'Bill',
                    'title' => 'Mr',
                    'salary' => 0
                ),
            );

            echo shHtml_2($sh);


//        l($htmlText);

        // ----------------------- test
        echo "<hr/>";
        $time1 = microtime(true);
        $test_parse_string_4 = 0;

//        f();

        $time2 = microtime(true);
        echo "<p>", $time1;
        echo "<p>", round($test_parse_string_4 / 1024 / 1024, 2);
        echo "<p>", $time2;
        echo "<p>", $time2 - $time1;

        // ----------------------- test
        echo "<hr/>";
        $time1 = microtime(true);
        $test_parse_string_4 = 0;
        for ($i = 0; $i < 20; $i++) {
//            executeTests();
        }

        $time2 = microtime(true);
        echo "<p>", $time1;
        echo "<p>", round($test_parse_string_4 / 1024 / 1024, 2);
        echo "<p>", $time2;
        echo "<p>", $time2 - $time1;
        ?>
        <!--        </pre>-->
    </body>
</html>

<?
# --------------- TESTS

        function executeTests() {
//    test1();
//    for($i = 1; $i <= 3; $i++){
//        $test = 'test_tag_'.$i;
//        $test();
//    }
//    for($i = 2; $i <= 8; $i++){
//        $test = 'test'.$i;
//        $test();
//    }
//    for($i = 1; $i <= 5; $i++){
//        $test = 'test_contr_'.$i;
//        $test();
//    }
            for ($i = 4; $i <= 4; $i++) {
                $test = 'test_parse_string_' . $i;
                $test();
            }
        }

        function test_tag_1() {
            echo "\nparsing tags 1\n";
            $s = 'table#what.wefw.sdf.we?fwe>test.class1?what#test1';
            $p = 0;
            $res = _parse_char($s, $p);
            var_export($res);
            echo "\n";
            var_export($p);
            echo "\n";
            echo strlen($s);
            echo "\n";
        }

        function test_tag_2() {
            echo "\nparsing tags 2\n";
            $s = 'table.sdf.we?fwe>';
            $p = 0;
            $res = _parse_char($s, $p);
            var_export($res);
            echo "\n";
            var_export($p);
            echo "\n";
            echo strlen($s);
            echo "\n";
        }

        function test_tag_3() {
            echo "\nparsing tags 3\n";
            $s = 'table.sdf.we?fwe>#test1|.test2';
            $p = 0;
            $res = _parse_char($s, $p);
            var_export($res);
            echo "\n";
            var_export($p);
            echo "\n";
            echo strlen($s);
            echo "\n";
        }

        function test2() {
            echo "\nparsing attr 2\n";
            $s = 'style: width: 12px; height: 23px,,';
            $p = 0;
            $res = _parse_char($s, $p);
            var_export($res);
            echo "\n";
            var_export($p);
            echo "\n";
            echo strlen($s);
            echo "\n";
        }

        function test3() {
            echo "\nparsing attr 3\n";
            $s = 'style: ';
            $p = 0;
            $res = _parse_char($s, $p);
            var_export($res);
            echo "\n";
            var_export($p);
            echo "\n";
            echo strlen($s);
            echo "\n";
        }

        function test4() {
            echo "\nparsing attr 4\n";
            $s = 'style: ~d';
            $p = 0;
            $res = _parse_char($s, $p);
            var_export($res);
            echo "\n";
            var_export($p);
            echo "\n";
            echo strlen($s);
            echo "\n";
        }

        function test5() {
            echo "\nparsing attr 5\n";
            $s = 'style: ~d/what2';
            $p = 0;
            $res = _parse_char($s, $p);
            var_export($res);
            echo "\n";
            var_export($p);
            echo "\n";
            echo strlen($s);
            echo "\n";
        }

        function test6() {
            echo "\nparsing attr 6\n";
            $s = 'style: /key_name';
            $p = 0;
            $res = _parse_char($s, $p);
            var_export($res);
            echo "\n";
            var_export($p);
            echo "\n";
            echo strlen($s);
            echo "\n";
        }

        function test7() {
            echo "\nparsing attr 7\n";
            $s = 'style: fwefad wewi fweij /,';
            $p = 0;
            $res = _parse_char($s, $p);
            var_export($res);
            echo "\n";
            var_export($p);
            echo "\n";
            echo strlen($s);
            echo "\n";
        }

        function test8() {
            echo "\nparsing attr 8\n";
            $s = 'message: hello/,, \' boy,,';
            $p = 0;
            $res = _parse_char($s, $p);
            var_export($res);
            echo "\n";
            var_export($p);
            echo "\n";
            echo strlen($s);
            echo "\n";
        }

        function test_contr_1() {
            echo "\nparsing contr 1\n";
            $s = 'for(';
            $p = 0;
            $res = _parse_char($s, $p);
            var_export($res);
            echo "\n";
            var_export($p);
            echo "\n";
            echo strlen($s);
            echo "\n";
        }

        function test_contr_2() {
            echo "\nparsing contr 2\n";
            $s = 'for(~d)';
            $p = 0;
            $res = _parse_char($s, $p);
            var_export($res);
            echo "\n";
            var_export($p);
            echo "\n";
            echo strlen($s);
            echo "\n";
        }

        function test_contr_3() {
            echo "\nparsing contr 3\n";
            $s = 'for(   /name )';
            $p = 0;
            $res = _parse_char($s, $p);
            var_export($res);
            echo "\n";
            var_export($p);
            echo "\n";
            echo strlen($s);
            echo "\n";
        }

        function test_contr_4() {
            echo "\nparsing contr 4\n";
            $s = 'if( ~s )';
            $p = 0;
            $res = _parse_char($s, $p);
            var_export($res);
            echo "\n";
            var_export($p);
            echo "\n";
            echo strlen($s);
            echo "\n";
        }

        function test_contr_5() {
            echo "\nparsing contr 5\n";
            $s = 'if( ~d/name )';
            $p = 0;
            $res = _parse_char($s, $p);
            var_export($res);
            echo "\n";
            var_export($p);
            echo "\n";
            echo strlen($s);
            echo "\n";
        }

        function test_parse_string_1() {
            echo "\nparsing string 1\n";
            $s = "table> tr>tr td/ &this is a <b>here</b> quick html& td/ :wef,, td/ tr>tr td/ td/ td/ tr> td/ td/ tdAttr: ~d td/ if(~s e) for(/what) test:";
            //$s = "table> tr>tr td/ :1,, td/ :2,, td/ :3,, tr>tr td/ :4,, td/ :5,, td/ :6,, tr> td/ :7,, td/ :8,, td/ :9,,";
            $stack = array();
            $array = array($s);
            $array[] = 12;
            $array[] = 'w';
            $array[] = null;
            $array[] = '/tpl_1 //eif  //efor :~s';

            $html = array('<b>', '<html>', '</b>');
            $array[] = $html;

            $shl = array(
                // table of the ...
                'table>.newTable',
                'tr>tr td/ :header1,, td/ :header2,,',
                'tr>tr td/ :name1,, td/ :name2,,',
                // table of ....
                'table.newTable>',
                'tr> td/ :header1,, td/ :header2,, <<',
                'tr>tr td/ :name1,, td/ :name2,, <2 <4 <<< < <table <#newTable.class1 <#test|.test2 <tag#table.class1|#what|#again',
                'tr>tr td>td &name & b/ &3& #this is a bold text# td/ :name4,,'
            );
            $array[] = $shl;

            $arrayIndex = 0;


            echo _parse_shl_code($stack, $array, $arrayIndex);


            var_export($stack);
            echo "\n";
        }

        function test_parse_string_2() {
            echo "\nparsing string 2\n";

            $stack = array();
            $array = array("table> pid: ~d tr>tr td/ for(/data) div> ", '3',
                ": /divContent ",
                "for(/subData) div> :/subDiv <div //efor ",
                "< //efor :~s/file ",
                "if(/what t) b/ :in if,, //eif",
                "a/ href:http://www.google.com,, :~s", 'quick link to google.com',
                '# comment #'
            );
            $array = array("link> ref:stylesheet,, type:text/css,, div/ :test div,, <link",
                "table> tr.headerTr> th/ :content,, th/ :test,, <<",
                "for(/data) tr.dataTr>tr id: ~s/trId ",
                "td.contentField/ :/divContent",
                "td.testField> a/ href:http://www.google.com,, :go to google,,",
                "#comment in a loop# style:border:1px #cf4 solid; padding:5px; margin:3px;font-size:23px,, <",
                "//efor");

            $arrayIndex = 0;

            echo _parse_shl_code($stack, $array, $arrayIndex);

            $data = array('file' => 'what the <b>bold</b> fuck!',
                'what' => 'f',
                'data' => array(
                    array('divContent' => 'content 1',
                        'subData' => array(
                            array('subDiv' => 'sub div 1'),
                            array('subDiv' => 'sub div 2'),
                            array('subDiv' => 'sub div 3'),
                    )),
                    array('divContent' => 'content 2', 'trId' => 4, 'subData' => ''),
                    array('divContent' => 'content 3',
                        'subData' => array(
                            array('subDiv' => 'sub div 4'),
                            array('subDiv' => 'sub div 5'),
                            array('subDiv' => 'sub div 6'),
                    )),
                    array('divContent' => 'content 4',
                        'subData' => array(
                            array('subDiv' => 'sub div 7'),
                    )),
                    ));
            $instrPtr = 0;
            $finalInstr = array();
            echo _populate_sh_instr($data, $stack, $instrPtr, $finalInstr);

            $tagStack = array();
            $stackIndex = 0;
            _compile_tree_obj($finalInstr, $tagStack, $stackIndex);

            $htmlText = array();
            _generate_html($tagStack[0]['content'], $htmlText, 0);

//    var_export($stack);
//    var_export($finalInstr);
//    var_export($tagStack[0]);
//    pr('-------------------------------------------');
//    var_export($htmlText);
            echo count($htmlText), "\n";
            pr('---------------------------------------------');
            echo implode('', $htmlText);


            echo "\n";
        }

        function test_parse_string_3() {
            $shObj = shCreate(
                            "table#staffList>",
                            "tr> th/ :person name,, th/ :role name,, <tr",
                            "for(/persons)",
                            "tr>tr pid:~d/pid",
                            "td/ :/personName",
                            "td/ :/roleName",
                            "//efor"
            );

            $person = &$shObj['persons'];

            $person[] = array('pid' => 3, 'personName' => 'pop', 'roleName' => 'developer');
            $person[] = array('pid' => 4, 'personName' => 'weng lock', 'roleName' => 'developer');
            $person[] = array('pid' => 5, 'personName' => 'john', 'roleName' => 'seniar lecturor');
            $person[] = array('pid' => 6, 'personName' => 'keryl', 'roleName' => 'coordinator');
            $person[] = array('pid' => 7, 'personName' => 'luke', 'roleName' => 'tester');
            $person[] = array('pid' => 8, 'personName' => 'robert', 'roleName' => 'user');


            echo shHtml($shObj);
            echo "<hr>";

            $shObj = shCreate(
                            'div/ #comment here# style:',
                            array(
                                'color: #dd3;',
                                'border: 2px solid #33d'
                            ),
                            ':', 'This is a test div',
                            '#comment 2# ',
                            'table#staffList> #comment 3#',
                            'tr>tr th/ :person name,, th/ :role name,,'
            );
            foreach ($person as $p) {
                shPush($shObj,
                        'tr>tr pid:', $p['pid'],
                        'td/ :', $p['personName'],
                        'td/ :', $p['roleName']
                );
            }

//    $shObj = shCreate()

            echo shHtml($shObj);
            echo "<hr>";

            $div = shCreate('div/ #comment here# style:',
                            array(
                                'color: #dd3;',
                                'border: 2px solid #33d'
                            ),
                            ':', 'This is another test'
            );
            $shObj = shCreate(
                            $div,
                            'table#staffList>',
                            'tr>tr th/ :person name,, th/ :role name,, <',
                            '/table <'
            );


            $table = shCreate();
            foreach ($person as $p) {
                shPush($table,
                        'tr>tr pid:', $p['pid'],
                        'td/ :', $p['personName'],
                        'td/ :', $p['roleName']
                );
            }
            $shObj['table'] = $table;
            echo shHtml($shObj);
            echo '<hr></hr>';

            echo shQuick('div> :this is a quick div,,',
                    'style:width:400px;border:1px solid #339;padding:5px;,,',
                    'a/ href:http://www.google.com,, target:_blank,, :', 'go to google');
        }

        function test_parse_string_4() {
            global $test_parse_string_4;

            $shObj = shCreate(
                            "table#staffList>",
                            "tr> th/ :person name,, th/ :role name,, <tr",
                            "for(/persons)",
                            "tr>tr pid:~d/pid",
                            "td/ :/personName",
                            "td/ :/roleName",
                            "//efor"
            );

            $person = &$shObj['persons'];

            $person[] = array('pid' => 3, 'personName' => 'pop', 'roleName' => 'developer');
            $person[] = array('pid' => 4, 'personName' => 'weng lock', 'roleName' => 'developer');
            $person[] = array('pid' => 5, 'personName' => 'john', 'roleName' => 'seniar lecturor');
            $person[] = array('pid' => 6, 'personName' => 'keryl', 'roleName' => 'coordinator');
            $person[] = array('pid' => 7, 'personName' => 'luke', 'roleName' => 'tester');
            $person[] = array('pid' => 8, 'personName' => 'robert', 'roleName' => 'user');


            $test_parse_string_4 += strlen(shHtml($shObj));
//            echo(shHtml($shObj));

            $shObj = shCreate(
                            'div/ #comment here# style:',
                            array(
                                'color: #dd3;',
                                'border: 2px solid #33d'
                            ),
                            ':', 'This is a test div',
                            '#comment 2# ',
                            'table#staffList> #comment 3#',
                            'tr>tr th/ :person name,, th/ :role name,,'
            );
            foreach ($person as $p) {
                shPush($shObj,
                        'tr>tr pid:', $p['pid'],
                        'td/ :', $p['personName'],
                        'td/ :', $p['roleName']
                );
            }

//    $shObj = shCreate()

            $test_parse_string_4 += strlen(shHtml($shObj));

            $div = shCreate('div/ #comment here# style:',
                            array(
                                'color: #dd3;',
                                'border: 2px solid #33d'
                            ),
                            ':', 'This is another test'
            );
            $shObj = shCreate(
                            $div,
                            'table#staffList>',
                            'tr>tr th/ :person name,, th/ :role name,, <',
                            '/table <'
            );


            $table = shCreate();
            foreach ($person as $p) {
                shPush($table,
                        'tr>tr pid:', $p['pid'],
                        'td/ :', $p['personName'],
                        'td/ :', $p['roleName']
                );
            }
            $shObj['table'] = $table;
            $test_parse_string_4 += strlen(shHtml($shObj));

            $test_parse_string_4 += strlen(shQuick('div> :this is a quick div,,',
                                    'style:width:400px;border:1px solid #339;padding:5px;,,',
                                    'a/ href:http://www.google.com,, target:_blank,, :', 'go to google'));
        }